# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community
Edition) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
from django.http import JsonResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from .models import Images


# 开发框架中通过中间件默认是需要登录态的，如有不需要登录的，可添加装饰器login_exempt
# 装饰器引入 from blueapps.account.decorators import login_exempt
def home(request):
    """
    首页
    """
    return render(request, "home_application/index_home.html")


def dev_guide(request):
    """
    开发指引
    """
    return render(request, "home_application/dev_guide.html")


def contact(request):
    """
    联系页
    """
    return render(request, "home_application/contact.html")


def backup_operation(request):
    """
    备份操作
    """
    return render(request, "home_application/backup_operation.html")


def backup_records(request):
    """
    备份记录
    """
    return render(request, "home_application/backup_records.html")


def helloworld(request):
    return render(request, "home_application/helloworld.html", {"username": request.user.username})


def show_homework(request):
    from .practices.practice_basic.homework1_2 import this_is_homework2
    return render(request, "home_application/homework1.2.html", {"index": this_is_homework2()})


def show_homework2_2(request):
    return render(request, "home_application/homework2.2.html")


def show_homework3(request):
    return render(request, "home_application/homework3.html")


def frontend(request):
    return render(request, "home_application/frontend.html", {"username": request.user.username})


def media_home(request):
    # 获取数据库前20个图片
    images = Images.objects.all()[:20]
    image_list = [{"image_url": image.image.url, "image_id": image.id} for image in images]
    return render(request, "home_application/download_upload.html", {"image_list": image_list})


def download(request, image_id):
    """
    下载图片
    """
    image = Images.objects.filter(id=image_id).first()
    # 获取图片的url地址，传给前端
    url = image.image.url
    return JsonResponse({"result": True, "code": 200, "data": {"url": url}})


def upload(request):
    """
    上传图片
    """
    # 获取图片
    uploaded_file = request.FILES.get("pics")
    if uploaded_file:
        # 如果图片存在则将该图片信息通过ImageField存入数据库
        Images.objects.create(image=uploaded_file)
    # 当图片存储之后，重定向到首页
    return HttpResponseRedirect(reverse("doc:media_home"))
